package com.example.demoauthservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.oauth2.client.OAuth2RestOperations;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@CrossOrigin(value = "*")    //支持跨域访问，implicit授权模式访问时须配
public class MyServiceController {

    @Autowired
    private DemoAuthServiceApplication.RepositoryFeign repositoryFeign;

    @GetMapping("/product/{id}")
    public String getProduct(@PathVariable String id) {
        return "product id : " + id;
    }

    @GetMapping("/order/{id}")
    public String getOrder(@PathVariable String id) {
        return "order id : " + id;
    }

    @GetMapping("/order/repo/{id}")
    public String getOrderRepo(@PathVariable String id) {
        System.out.println("repository============");

//        String msg=aRestTemplate.getForEntity("http://localhost:8791/repository/"+id,String.class).getBody();
//        return msg;

        return repositoryFeign.getRepository(id);
    }

    @PreAuthorize("principal.username.equals('user_2')")
    public String hello() {
        return "hello";
    }

    @PreAuthorize("hasRole('admin')")
    public String admin() {
        return "admin";
    }

    @Secured({"ROLE_user"})
    public String user() {
        return "user";
    }

    @PreAuthorize("#age>28")
    public String getAge(Integer age) {
        return String.valueOf(age);
    }
}
